<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\BaseController;
use think\facade\View;
use app\common\model\Product\Product as ProductModel;
use app\common\model\Product\Vip as VipModel;
use think\exception\ValidateException;

class Vip extends BaseController
{
    protected $middleware = [
        Auth::class
    ];

    protected function initialize()
    {
        parent::initialize();

        // 实例化模型
        $this->VipModel = new VipModel();
    }

    public function index()
    {
        $nav_title = [
            'nav_title' => '商品管理',
            'title' => '商品等级价格'
        ];

        $proid = $this->request->param('proid');

        $product = ProductModel::find($proid);

        if(!$product)
        {
            $this->error('商品不存在');
        }

        $where[] = ['proid','=',$proid];

        $VipList = $this->VipModel->where($where)->paginate(['list_rows' => 5, 'query' => request()->param()]);

        // 编号 currentPage 当前页 listRows 每页要显示多少条数据
        $num = ($VipList->currentPage() - 1) * $VipList->listRows();

        $nav_title['title'] = $product['name'] . '商品等级价格';

        return View::fetch('',[
            'nav_title' => $nav_title,
            'proid' => $proid,
            'num' => $num,
            'VipList' => $VipList
        ]);
    }

    public function add()
    {
        $nav_title = [
            'nav_title' => '商品管理',
            'title' => '新增商品等级价格'
        ];

        $proid = $this->request->param('proid');

        $product = ProductModel::find($proid);

        if(!$product)
        {
            $this->error('商品不存在');
        }

        $nav_title['title'] = '新增'.$product['name'] . '商品等级价格';

        if($this->request->isPost())
        {
            $params = $this->request->param();

            $data = [
                'proid' => $proid,
                'price' => $params['price'],
                'level' => $params['level']
            ];

            try {
                validate(\app\common\validate\Product\Vip::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getError());
            }

            $result = $this->VipModel->save($data);

            if($result !== true)
            {
                $this->error('新增失败');
            }else{
                $this->success('新增成功',(String)url('admin/vip/index',['proid' => $proid]));
            }

        }

        return View::fetch('',[
            'nav_title' => $nav_title
        ]);
    }

    public function edit()
    {
        $nav_title = [
            'nav_title' => '商品管理',
            'title' => '新增商品等级价格'
        ];

        $proid = $this->request->param('proid');

        $vipid = $this->request->param('vipid');

        $product = ProductModel::find($proid);

        $vip = $this->VipModel->find($vipid);

        if(!$product || !$vip)
        {
            $this->error('商品不存在');
        }

        if($this->request->isPost())
        {
            $params = $this->request->param();

            $data = [
                'price' => $params['price'],
                'level' => $params['level']
            ];

            $result = $vip->save($data);

            if($result !== true)
            {
                $this->error('更新失败');
            }else{
                $this->success('更新成功',(String)url('admin/vip/index',['proid' => $proid]));
            }

        }

        $nav_title['title'] = '编辑'.$product['name'] . '商品等级价格';

        return View::fetch('',[
            'nav_title' => $nav_title,
            'vip' => $vip
        ]);
    }

    public function del()
    {
        if($this->request->isAjax())
        {
            $vipid = $this->request->param('vipid');

            if(empty($vipid))
            {
                $this->error('请选择需要删除记录');
            }

            if(is_array($vipid))
            {
                // 批量删除操作

                // 查询数据
                $idArr = $this->VipModel->whereIn('id',$vipid)->column('id');

                $NotId = '';
                
                foreach ($vipid as $item) {
                    if(!in_array($item,$idArr))
                    {
                        $NotId .= $item . ',';
                    }
                }
                
                if(!empty($NotId))
                {
                    $NotId = rtrim($NotId,',');
                    $this->error("ID：{$NotId}不存在，请重新选择");
                }

                $result = $this->VipModel->destroy($vipid);

                if($result !== true)
                {
                    $this->error('批量删除失败');
                }else{
                    $this->success('批量删除成功');
                }
            }else{
                $vip = $this->VipModel->find($vipid);

                if(!$vip)
                {
                    $this->error('该记录不存在');
                }

                $result = $this->VipModel->destroy($vipid);

                if($result !== true)
                {
                    $this->error('删除失败');
                }else{
                    $this->success('删除成功');
                }
            }
        }
    }

    
}
